package com.kkb.interceptors;

import com.kkb.entity.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class OnlineInterceptor implements HandlerInterceptor {
    private static String userName = "";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        System.out.println(request.getRequestURI());
        User user = (User) session.getAttribute("user");
        if (user == null || user.getUsername() != userName){
            System.out.println("wendenglu");
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("<script>alert('请先登录账户!');location.href='/toLogin.do';</script>");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
